package com.drinks.yiniang.ui.activity.user.setting;

import android.os.Bundle;
import android.view.View;

import com.drinks.yiniang.R;
import com.drinks.yiniang.entity.user.UserSetJson;
import com.drinks.yiniang.io.api.UserApiIO;
import com.drinks.yiniang.ui.fragment.user.password.FirstPayPasswordSetFragment;
import com.drinks.yiniang.ui.fragment.user.password.ModifyPayPasswordFragment;
import com.drinks.yiniang.ui.fragment.user.password.VerifyPayPasswordFragment;
import com.drinks.yiniang.utils.SharedPreUtils;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.Constants;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.ui.fragment.BaseFragment;
import com.hansen.library.ui.activity.BaseTranBarActivity;
import com.hansen.library.listener.OnNavgationBarClickListener;
import com.hansen.library.ui.widget.nav.NavigationBarLayout;

/**
 * 支付设置页面
 *
 * @author han
 * @date 2019/09/08
 */
public class PaymentSettingActivity extends BaseTranBarActivity implements OnNavgationBarClickListener,
        VerifyPayPasswordFragment.OnPayPasswordVerifyListener {

    //nav
    private NavigationBarLayout nav_bar_payment_setting;

    private FirstPayPasswordSetFragment mFirstPayPasswordSetFragment;
    private VerifyPayPasswordFragment mVerifyPayPasswordFragment;
    private ModifyPayPasswordFragment mModifyPayPasswordFragment;

    private BaseFragment mCurrentFragment;

    @Override
    protected void initView() {
        nav_bar_payment_setting = findViewById(R.id.nav_bar_payment_setting);

    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mContext = this;

        getUserSettingData();
    }

    @Override
    protected void initListener() {
        nav_bar_payment_setting.setOnNavgationBarClickListener(this);
    }

    @Override
    public void widgetClick(View v) {

    }

    @Override
    public int getTranBarResID() {
        return R.layout.activity_payment_setting;
    }

    @Override
    public void onBackClick(View v) {
        finish();
    }

    @Override
    public void onEditClick(View v) {

    }

    @Override
    public void onPayPasswordVerifySuccess() {
        toModifyPage();
    }

    @Override
    public void onPayPasswordVerifyForgot() {//忘记密码后和第一次一样重置密码
        initFragment(true);
    }

    @Override
    public void onPayPasswordVerifyFail() {

    }

    private void initFragment(boolean isFirstSetPayPassword) {
        if (isFirstSetPayPassword) {//第一次设置密码
            if (mFirstPayPasswordSetFragment == null)
                mFirstPayPasswordSetFragment = FirstPayPasswordSetFragment.newInstance();

            mCurrentFragment = addFragment(mFirstPayPasswordSetFragment, R.id.fl_pay_pwd_setting);
        } else {//不是第一次设置密码，需要先验证密码
            if (mVerifyPayPasswordFragment == null)
                mVerifyPayPasswordFragment = VerifyPayPasswordFragment.newInstance();

            mCurrentFragment = addFragment(mVerifyPayPasswordFragment, R.id.fl_pay_pwd_setting);
        }
    }

    //修改密码页面
    private void toModifyPage() {
        if (mModifyPayPasswordFragment == null) {
            mModifyPayPasswordFragment = ModifyPayPasswordFragment.newInstance();
        }

        mCurrentFragment = switchFragment(mCurrentFragment, mModifyPayPasswordFragment, R.id.fl_pay_pwd_setting);
    }

    private void getUserSettingData() {
        String isOpenPayPassword = SharedPreUtils.getInstance().getIsOpenPayPassword();

        if (Constants.STATUS_GLOBAL_YES.equals(isOpenPayPassword) || Constants.STATUS_GLOBAL_NO.equals(isOpenPayPassword)) {
            initFragment(false);
            return;
        }

        showLoadingDialog();

        UserApiIO.getInstance().getUserSetData(new APIRequestCallback<UserSetJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(UserSetJson res) {
                if (Constants.STATUS_GLOBAL_YES.equals(res.getData().getIs_first())) {//第一次设置密码
                    initFragment(true);
                } else {
                    initFragment(false);
                    SharedPreUtils.getInstance().saveIsOpenPayPasswordID(Constants.STATUS_GLOBAL_YES.equals(res.getData().getOpen_pay()) ? Constants.STATUS_GLOBAL_YES : Constants.STATUS_GLOBAL_NO);
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }
}
